package cz.najman.emdroid.utils;

public class WhereClauseBuilder {		
	
	private StringBuilder mStrBuilder;
	private static WhereClauseBuilder _instance;
	
	private WhereClauseBuilder() {
		mStrBuilder = new StringBuilder();
	}
	
	public static WhereClauseBuilder getInstance(){
		if(_instance == null){
			_instance = new WhereClauseBuilder();
		}
		
		return _instance;
	}
	
	public String buildWhereClause(String column, String value, String operator){			
		mStrBuilder.delete(0, mStrBuilder.length());
		appendExpression(column, value, operator);			
		
		return mStrBuilder.toString();
	}
	
	public String buildWhereClause(String column, int value, String operator){			
		mStrBuilder.delete(0, mStrBuilder.length());
		String sValue = String.valueOf(value);
		appendExpression(column, sValue, operator);			
		
		return mStrBuilder.toString();
	}
	
	public String appendWhereClause(String existingWhereClause, String column, String value, String operator, String joinOperation){
		mStrBuilder.delete(0, mStrBuilder.length());
		mStrBuilder.append(existingWhereClause);
		mStrBuilder.append(joinOperation);		
			
		appendExpression(column, value, operator);
		
		return mStrBuilder.toString();		
		
	}
	
	public String appendWhereClause(String existingWhereClause,String joinOperator, String[] columns, String[] values, String[] operators, String[] joinOperators){
		mStrBuilder.delete(0, mStrBuilder.length());
		mStrBuilder.append(existingWhereClause);
		mStrBuilder.append(joinOperator);		
			
		if( (columns.length == values.length) && (columns.length == operators.length) && (columns.length == (joinOperators.length+1)) ){
			int i=0;
			int lenght = columns.length;
			while(i < lenght){
				appendExpression(columns[i], values[i], operators[i]);
				i++;
				if(i < lenght){				
					mStrBuilder.append(AND);
				}			
			}
			
			return mStrBuilder.toString();
		} else {
			throw new RuntimeException("Wrong number of paramaters to build the where clause.");
		}			
		
	}
	
	public String buildWhereClause(String[] columns, String[] values, String[] operators){
		
		int lenght = columns.length;
		String[] joinOperators = new String[lenght-1];
		for(int i = 0; i < lenght-1;i++ ){
			joinOperators[i] = AND;
		}
		
		return buildWhereClause(columns, values, operators, joinOperators);
	}
	
	public String buildWhereClause(String[] columns, String[] values, String[] operators, String[] joinOperators){
		mStrBuilder.delete(0, mStrBuilder.length());
		
		if( (columns.length == values.length) && (columns.length == operators.length) && (columns.length == (joinOperators.length+1)) ){
			int i=0;
			int lenght = columns.length;
			while(i < lenght){
				appendExpression(columns[i], values[i], operators[i]);
				i++;
				if(i < lenght){				
					mStrBuilder.append(AND);
				}			
			}
			
			return mStrBuilder.toString();
		} else {
			throw new RuntimeException("Wrong number of paramaters to build the where clause.");
		}		
	}
	
	private void appendExpression(String column, String value, String operator){
		mStrBuilder.append(column);
		mStrBuilder.append(operator);
		mStrBuilder.append(value);
	}
	
	public static final String EQUAL = " = ";
	public static final String NOT_EQUAL = " <> ";
	public static final String GRATER_THEN = " > ";
	public static final String LESS_THEN = " < ";
	public static final String GRATER_THEN_OR_EQUAL = " >= ";
	public static final String LESS_THEN_OR_EQUAL = " <= ";
	public static final String BETWEEN = " BETWEEN ";
	public static final String LIKE = " LIKE ";
	public static final String IN = " IN ";
	public static final String OR = " OR ";
	public static final String AND = " AND ";
}